package com.service.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.gmall.model.product.BaseAttrInfo;
import com.service.gmall.model.product.BaseAttrValue;
import com.service.gmall.product.mapper.BaseAttrValueMapper;
import com.service.gmall.product.service.BaseAttrInfoService;
import com.service.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author alpha
 * @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
 * @createDate 2022-08-23 10:15:19
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {
    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> info = baseAttrInfoMapper.getAttrInfoList(category1Id, category2Id, category3Id);
        return info;
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo info) {
        if (info.getId() == null) {
            //如果前台传入数据的id为空，说明是新增操作
            addBaseAttrInfo(info);
        } else {
            /* 否则就是修改操作
            要注意的一点是这里涉及两张表哦
            修改操作涉及到新增元素，修改元素，删除元素
            新增时机：数据库原本不存在的，也就是说attrId为空
            修改时机：数据库原本就存在的，也就是说attrId不为空
            删除时机：数据库中原本存在，但是修改后删掉了，判断条件：attrId=xxx and id not in (xxx)*/
            updateBaseAttrInfo(info);
        }

    }

    private void updateBaseAttrInfo(BaseAttrInfo info) {
        //属性值修改
        baseAttrInfoMapper.updateById(info);
        List<BaseAttrValue> valueList = info.getAttrValueList();

        //统计BaseAttrValue的id
        List<Long> bids = new ArrayList<>();
        for (BaseAttrValue value : valueList) {
            Long id = value.getId();
            if (id != null) {
                bids.add(id);
            }
        }

        // delete * from base_attr_value where attr_id=11 and id not in(59,61)
        if (!bids.isEmpty()) {
            //不为空,部分删除
            baseAttrValueMapper.delete(new LambdaQueryWrapper<BaseAttrValue>()
                    .eq(BaseAttrValue::getAttrId, info.getId())
                    .notIn(BaseAttrValue::getId,bids));
        }else {
            //全删除
            baseAttrValueMapper.delete(new LambdaQueryWrapper<BaseAttrValue>()
                    .eq(BaseAttrValue::getAttrId, info.getId()));
        }

        for (BaseAttrValue value : valueList) {
            if (value.getAttrId() == null) {
                //attrId为空，说明是新增,进行id回填
                value.setAttrId(info.getId());
                baseAttrValueMapper.insert(value);
            } else if (value.getAttrId() != null) {
                //attrId不为空，说明是修改
                baseAttrValueMapper.updateById(value);
            }
        }
    }

    private void addBaseAttrInfo(BaseAttrInfo info) {
        //1. 解析要存入BaseAttrInfo中的数据
        baseAttrInfoMapper.insert(info);
        Long id = info.getId();
        //2. 解析要存入BaseAttrValue中的数据
        List<BaseAttrValue> attrValueList = info.getAttrValueList();
        for (BaseAttrValue value : attrValueList) {
            //回填自增id值
            value.setAttrId(id);
            baseAttrValueMapper.insert(value);
        }
    }
}




